# -*- coding: utf-8 -*-
import requests
from lxml import etree
from tools.mysqlconn import NBAdb
from mysql_text import sql_insert_text
import re


class Salary:

    def __init__(self):
        """
        初始化一些东西
        """
        # 提取team参数
        one = NBAdb()
        select_sql = """
                SELECT bi_English_name
                FROM team_brief_introduction
                ORDER BY bi_id;
                """
        list_one = one.select_data(select_sql)
        self.list_team = []
        for item in list_one:
            for i in item:
                self.list_team.append(i[-4:-1])
        # print(self.list_team)
        # 释放资源
        del one

        # 初始化请求头
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
        }

    def get_salary(self):
        """
        获取球队历年薪金状况数据、球队历年薪金状况总计数据；
        2个表。
        :return:
        """
        two = NBAdb()

        # 提取season参数（建立在提取team参数的前提上）
        for team in self.list_team[1:]:
            url = 'http://www.stat-nba.com/team/{}.html'.format(team)
            res = requests.get(url=url, headers=self.headers)
            res.encoding = 'utf-8'
            # print(res.status_code)
            html = etree.HTML(res.text)

            if html.xpath('//div[@id="salaryT"]'):
                content_one = html.xpath('//div[@id="salaryT"]/../../div[2]/a/@id')

                for a in content_one:

                    if a != 'change_season_salarycurrent':

                        season = (re.findall(r'\d+', a))[0]

                        # 建立参数
                        params = {
                            'name_eng_short': team,
                            'season': season
                        }
                        url_season = 'http://www.stat-nba.com/team/salary.php'
                        res_season = requests.get(url_season, params=params, headers=self.headers)
                        res_season.encoding = 'utf-8'
                        # print(res_season.status_code)
                        html_season = etree.HTML(res_season.text)

                        # 球队历年薪金状况表
                        content_two = html_season.xpath('//table/tbody/tr')
                        for i in content_two[:-2]:
                            list_zj = [team, season]
                            list_ls = i.xpath('.//td[position()<4]//text()')
                            list_zj.extend(list_ls)
                            two.insert_table(sql_insert_text.insert_team_salary_situation, list_zj)

                        # 球队历年薪金总计表
                        for i in content_two[-1:]:
                            list_zj = [team, season]
                            list_ls = i.xpath('.//td[2]//text()')
                            list_zj.extend(list_ls)
                            two.insert_table(sql_insert_text.insert_team_salary_count, list_zj)

        del two


if __name__ == '__main__':
    print('ok')
    # Salary().get_salary()
